# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: 写文件操作.py
Author: kij
Create Date: 2021/2/6
-------------------------------------------------
"""
# 打开文件
# 路径 test2.txt
# 模式 wb（写入内容，b为字节 需要讲写的内容转成字节）
# 关闭文件
import os
import requests

# 获取绝对路径
file_path = os.path.dirname(os.path.abspath(__file__))
# # 打开
# file_object = open(os.path.join(file_path, 'files', 'test2.txt'),mode='wb')
# # 写入内容
# file_object.write('我的天能不能谁发的写入啊'.encode('utf-8'))
# # 关闭文件
# file_object.close()
# file_object = open(os.path.join(file_path, 'files', 'test2.txt'), mode='wt')
# file_object.write('直接写入不需要写编码格式看看啥效果')
# file_object.close()

# # 复制文件 可以写文件  一张图片 写入到另一张图片  没图片 拿文本文件代替
# f1 = open(os.path.join(file_path, 'files', 'test2.txt'), mode='rb')
# content = f1.read()
# f1.close()
#
# f2 = open(os.path.join(file_path, 'files', 'test3.txt'), mode='wb')
# f2.write(content)
# f2.close()

# # 获取网络json文件
# f2 = open(os.path.join(file_path, 'files', 'test4.txt'), mode='wb')
# res = requests.get(
# 	url="https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&page_start=20",
# 	headers={
# 		"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
# 	}
# )
# # 二进制原始文件 bytes
# f2.write(res.content)
# f2.close()
# f3 = open(os.path.join(file_path, 'files', 'test4.txt'), mode='rt')
# message = f3.read()
# f3.close()
# print(message)

# file_object = open(os.path.join(file_path, 'files', 'test1.txt',),mode='a+')
# while True:
# 	user_name = input("输入用户名:").strip()
# 	if user_name.upper() == "Q":
# 		break
# 	user_password = input("输入用户密码:").strip()
# 	file_object.write("{}-{}\n".format(user_name,user_password))
# file_object.close()

# # for 循环读取
# file_object = open(os.path.join(file_path,"files","test1.txt"),mode="rt")
# for line in file_object:
# 	print(line)
# file_object.close()


# file_object = open(os.path.join(file_path,"files","test1.txt"),mode="rt",encoding="utf-8")
# file_object = open(os.path.join(file_path,"files","test1.txt"),mode="rb")
# file_object.read(3)
# 获取光标位置
# print(file_object.tell())
# file_object.close()

# 避免忘记关闭的方法 支持多个文件
with open(os.path.join(file_path, "files", "test1.txt"), mode="rt") as file1, 	open(os.path.join(file_path, "files", "test3.txt"), mode="rt") as file2:
# with open(os.path.join(file_path,"files","test1.txt"),mode="rt") as file1:
	message = file1.read(10)
	print(message)
	message2 = file2.read()
	print("文件3",message2)
